package com.javasm.crm.service.aftersale.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.crm.domin.entity.aftersale.RefacOutGoods;
import com.javasm.crm.domin.entity.repository.Defective;
import com.javasm.crm.mapper.aftersale.RefacOutGoodsMapper;
import com.javasm.crm.service.aftersale.IRefacOutGoodsService;
import com.javasm.crm.service.repository.IDefectiveService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lirui
 * @since 2021-07-11
 */
@Service
@Transactional
public class RefacOutGoodsServiceImpl extends ServiceImpl<RefacOutGoodsMapper, RefacOutGoods> implements IRefacOutGoodsService {

    @Resource
    private IDefectiveService defectiveService;

    @Override
    public List<Defective> getByApplyId(Integer odoId) {
        QueryWrapper<RefacOutGoods> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<RefacOutGoods> lambda = wrapper.lambda();

        if (odoId != null && odoId > 0){
            lambda.eq(RefacOutGoods::getOdoId, odoId);
        }

        List<RefacOutGoods> list = list(wrapper);

        if (list.isEmpty())
            return new ArrayList<>();

        List<String> imeiList = new ArrayList<>();

        for (RefacOutGoods goods : list) {
            imeiList.add(goods.getGoodImei());
        }

        QueryWrapper<Defective> defectiveQueryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Defective> defectiveLambdaQueryWrapper = defectiveQueryWrapper.lambda();

        defectiveLambdaQueryWrapper.in(Defective::getGoodImei,imeiList);

        List<Defective> defectives = defectiveService.list(defectiveLambdaQueryWrapper);

        return defectives;
    }

    @Override
    public List<RefacOutGoods> getByApplyIdFromOutApply(Integer odoId) {

        QueryWrapper<RefacOutGoods> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<RefacOutGoods> lambda = wrapper.lambda();

        if (odoId != null && odoId > 0){
            lambda.eq(RefacOutGoods::getOdoId, odoId);
        }

        List<RefacOutGoods> list = list(wrapper);

        return list;
    }
}
